package com.txh.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.txh.blog.bean.Tags;
import com.txh.blog.bean.Type;
import com.txh.blog.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class TagsController{
    @Autowired
    TagsService tagsService;

    @RequestMapping("/tags")
    public ModelAndView type(@RequestParam(name = "page",required = true,defaultValue = "1") int page, @RequestParam(name = "pageSize",required = true,defaultValue = "4")int pageSize){
        List<Tags> all = tagsService.findAll(page, pageSize);
        PageInfo<Tags> info=new PageInfo<>(all);
        ModelAndView mv=new ModelAndView();
        mv.addObject("pageInfo",info);
        mv.setViewName("admin/tags");
        return mv;
    }
    @RequestMapping("/tag/save")
    @ResponseBody
    public String save(Tags tags){
        Tags tag = tagsService.findByName(tags.getName());
        if (tag != null) {
            return "1";
        }
        tagsService.save(tags);
        return "0";

    }
    @RequestMapping("/tag/delete")
    public String delete(@RequestParam Long id){
        tagsService.delete(id);
        return "redirect:/admin/tags";
    }
    @RequestMapping("/tag/findById")
    public ModelAndView findById(@RequestParam Long id){
        Tags tags = tagsService.findById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("tags",tags);
        mv.setViewName("admin/tags-input");
        return mv;
    }
    @RequestMapping("/tag/update")
    public String update(Tags tags){
        tagsService.update(tags);
        return "redirect:/admin/tags";
    }
}
